window.onload = function () {
    InitTimeDisplay();
    document.getElementById("table-name-input").value = GetParams("tableName");
    document.getElementById("search-text-input").value = GetParams("keyword");
    Search(GetParams("tableName"), GetParams("keyword"));
}

function GetInfo() {
    let infoBox = event.currentTarget.parentNode;
    let tableName = GetParams("tableName");
    let rowKey = infoBox.getElementsByClassName("row-key-text")[0].innerHTML;
    window.location.href = "tableView.html?init=show&tableName=" + tableName + "&rowKey=" + rowKey;
}

function AddInfo(rowKey, column, value) {
    let infoBoxOuter = document.getElementById("info-box-outer");
    let infoBox = document.createElement("div");
    infoBox.setAttribute("class", "info-box");
    infoBox.innerHTML = `
        <label class="row-key-label">行键: </label>
        <label class="row-key-text">` + rowKey + `</label>
        <label class="column-label">列名: </label>
        <label class="column-text">` + column + `</label>
        <button class="open-button" onclick="GetInfo()">></button>
        <br>
        <p class="value-content">` + value + `</p>`;
    infoBoxOuter.appendChild(infoBox);
}

function OnSearchButtonClicked()
{
    let tableName = document.getElementById("table-name-input").value;
    let keyword = document.getElementById("search-text-input").value;
    if (tableName === "") {
        ShowAlertOverlay("表名不能为空.");
        return
    }
    if (keyword === "") {
        ShowAlertOverlay("搜索关键字不能为空.");
        return;
    }
    window.location.href = "searchResult.html?tableName=" + tableName + "&keyword=" + keyword;
}

function Search(tableName, keyword) {
    let url = baseUrl + "/search/" + tableName + "?keyword=" + keyword + "&from=0&size=10";
    let http = new XMLHttpRequest();
    http.open("GET", url);
    http.send();
    http.onreadystatechange = function () {
        if (this.readyState === http.DONE) {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status) {
                case 0:
                    ShowAlertOverlay("无法连接到远端服务器!");
                    CloseLoadingOverlay();
                    break;
                case 200:
                    let result = resJson["Result"];
                    for (let i = 0; i < result.length; i++)
                        AddInfo(result[i]["rowKey"], result[i]["column"], result[i]["value"]);
                    if (resJson.Warning.length !== 0) {
                        for (let i = 0; i < resJson.Warning.length; i++) {
                            displayText += resJson.Warning[i];
                            displayText += "<br/>";
                        }
                    }
                    if (result.length === 0) {
                        displayText += "未检索到相关信息";
                        displayText += "<br/>";
                    }
                    if (displayText !== "")
                        ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
                default:
                    displayText += "Code: ";
                    displayText += this.status;
                    displayText += " ";
                    displayText += this.statusText;
                    displayText += "<br/>";
                    for (let i = 0; i < resJson.Warning.length; i++) {
                        displayText += resJson.Warning[i];
                        displayText += "<br/>";
                    }
                    displayText += resJson.Error;
                    displayText += "<br/>";
                    ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
            }
        }
    };
    ShowLoadingOverlay();
}